package com.sskj.gocoin;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.base.App;
import com.sskj.common.util.SPUtil;
import com.sskj.gocoin.adapter.slimadapter.IViewInjector;
import com.sskj.gocoin.adapter.slimadapter.SlimAdapter;
import com.sskj.gocoin.adapter.slimadapter.SlimInjector;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.common.rxbus2.RxBus;
import com.sskj.gocoin.common.rxbus2.Subscribe;
import com.sskj.gocoin.common.rxbus2.ThreadMode;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.component.DaggerUserDataComponent;
import com.sskj.gocoin.m.bean.FabiPublishListBean;
import com.sskj.gocoin.mvchelper.ModelRx2DataSource;
import com.sskj.gocoin.v.activity.FabiSellDetail2Activity;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.lib.Constants;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 交易记录的activity
 */
public class FabiRecordActivity extends BaseActivity<FabiRecord2Presenter> {

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R2.id.toolBar)
    ToolBarLayout toolBarLayout;

    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private SlimAdapter<FabiPublishListBean> slimAdapter;
    private MVCCoolHelper<List<FabiPublishListBean>> mvcCoolHelper;


    @Override
    protected int getLayoutResId() {
        return R.layout.fabi_record_activity;
    }

    @Override
    public FabiRecord2Presenter getPresenter() {
        return new FabiRecord2Presenter();
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, FabiRecordActivity.class);
        context.startActivity(starter);
    }

    @Override
    public void initView() {
        super.initView();
        RxBus.getDefault().register(this);
        toolBarLayout.setLeftButtonOnClickLinster(v -> finish());
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        HashMap<String, String> statusMap = new HashMap<>();
        statusMap.put("1", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment2));
        statusMap.put("2", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment4));
        statusMap.put("3", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment6));
        statusMap.put("4", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment8));
        statusMap.put("5", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment10));
        statusMap.put("6", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment12));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
//                .setFirstDraw(false)
//                .setLastDraw(false)
//                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 20))
//                .setDividerColor(ContextCompat.getColor(this, R.color.commonTextEEE)));
        slimAdapter = SlimAdapter.create().register(R.layout.recy_item_fabi_record, new SlimInjector<FabiPublishListBean>() {
            @Override
            public void onInject(FabiPublishListBean data, IViewInjector injector) {

                injector
                        .text(R.id.tv_order_number, data.getOrder_num())
                        .text(R.id.tv_order_status, statusMap.get(data.getCode()))
                        .text(R.id.tv_order_type, data.isBuyer() ? App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment13) : App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment14))
                        .textColor(R.id.tv_order_type, data.isBuyer() ? ContextCompat.getColor(FabiRecordActivity.this, R.color.minefabi_05c) : ContextCompat.getColor(FabiRecordActivity.this, R.color.commonSelectColor))
                        .text(R.id.tv_order_price, data.getPrice())
                        .text(R.id.tv_order_amount, data.getTotal_num())
                        .text(R.id.tv_deal_money, data.getTotal_price() + "CNY")
                        .text(R.id.tv_order_date, data.getAdd_time())
                        .text(R.id.tv_name, data.getName1())
                        .text(R.id.tv_xiane, data.getMin_price() + "-" + data.getMax_price());
            }
        });
//        ItemClickSupport.addTo(recyclerView)
//                .setOnItemClickListener((recyclerView, position, v) -> {
//                    FabiSellDetailActivity.start(this, slimAdapter.getDataItem(position).getOrder_num(), isBuy);
//                });
        slimAdapter.attachTo(recyclerView).updateData(new ArrayList());
//        Flowable.rangeLong(0, 8)
//                .map(i -> i + "")
//                .toList()
//                .subscribe(strings -> slimAdapter.updateData(strings));
//        ModelRx2DataSource<FabiListBean> dataSource =
//                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<FabiListBean>)
//                        page -> mPresenter.getFabiList(page, Constants.PAGE_SIZE, isBuy), Constants.PAGE_SIZE);
//        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
//        mvcCoolHelper.setDataSource(dataSource);
//        mvcCoolHelper.setAdapter(slimAdapter);
//        mvcCoolHelper.refresh();

        ModelRx2DataSource<FabiPublishListBean> dataSource =
                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<FabiPublishListBean>)
                        page -> mPresenter.getData(page));
        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, position, v) -> {
                    if (System.currentTimeMillis() - clickTime < 1500L) {
                        return;
                    }
                    clickTime = System.currentTimeMillis();
                    String account = SPUtil.get(Constants.SP_ACCOUNT, "");
                    FabiSellDetail2Activity.start(FabiRecordActivity.this, slimAdapter.getDataItem(position).getOrder_num(), account.equals(slimAdapter.getDataItem(position).getGou_account()));
                });
    }

    //item点击事件防止重点击
    private long clickTime = 0;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }

    @Subscribe(code = RxBusCode.OREDER_DETAIL_REFASH,threadMode = ThreadMode.MAIN)
    public void refresh(){
        mvcCoolHelper.refresh();
    }
}
